package org.giovanolli.mumi.dao.impl;

import java.util.List;

import org.giovanolli.mumi.dao.SpecialistDao;
import org.giovanolli.mumi.entities.Specialist;
import org.giovanolli.mumi.entities.filter.EntityFilter;
import org.giovanolli.mumi.entities.filter.SortCriteria;
import org.giovanolli.mumi.entities.filter.SpecialistFilter;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * The Class SpecialistDaoImpl.
 */
@Repository("specialistDao")
public class SpecialistDaoImpl extends HibernateDao<Specialist> implements SpecialistDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.dao.impl.HibernateDao#fillCriteria(org.giovanolli.mumi.entities.filter.EntityFilter, org.hibernate.criterion.DetachedCriteria)
	 */
	@Override
	protected DetachedCriteria fillCriteria(EntityFilter<Specialist> filter, DetachedCriteria criteria) {
		final SpecialistFilter entityFilter = (SpecialistFilter) filter;

		if (filter != null) {
			if (entityFilter.getFirstName() != null) {
				criteria.add(Restrictions.ilike("firstName", entityFilter.getFirstName(), MatchMode.ANYWHERE));
			}
			if (entityFilter.getFirstName() != null) {
				criteria.add(Restrictions.ilike("lastName", entityFilter.getLastName(), MatchMode.ANYWHERE));
			}
		}
		return criteria;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.dao.impl.HibernateDao#setDefaultSortCriteria(java.util.List)
	 */
	@Override
	protected void setDefaultSortCriteria(List<SortCriteria> defaultSortCriteria) {
		defaultSortCriteria.add(new SortCriteria("lastName", true));
		defaultSortCriteria.add(new SortCriteria("firstName", true));
	}

}
